package org.ruoyi.gathe.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.core.utils.StringUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.gathe.domain.GatheTable;
import org.ruoyi.gathe.domain.bo.GatheTableBo;
import org.ruoyi.gathe.domain.vo.GatheTableVo;
import org.ruoyi.gathe.mapper.GatheTableMapper;
import org.ruoyi.gathe.service.IGatheTableService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采集数据主Service业务层处理
 *
 * @author bz_huaYal9146
 * @date 2025-11-06
 */
@RequiredArgsConstructor
@Service
public class GatheTableServiceImpl implements IGatheTableService {

    private final GatheTableMapper baseMapper;

    /**
     * 查询采集数据主
     *
     * @param id 主键
     * @return 采集数据主
     */
    @Override
    public GatheTableVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询采集数据主列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 采集数据主分页列表
     */
    @Override
    public TableDataInfo<GatheTableVo> queryPageList(GatheTableBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GatheTable> lqw = buildQueryWrapper(bo);
        Page<GatheTableVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的采集数据主列表
     *
     * @param bo 查询条件
     * @return 采集数据主列表
     */
    @Override
    public List<GatheTableVo> queryList(GatheTableBo bo) {
        LambdaQueryWrapper<GatheTable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GatheTable> buildQueryWrapper(GatheTableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GatheTable> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(GatheTable::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getGatheTable()), GatheTable::getGatheTable, bo.getGatheTable());
        lqw.like(StringUtils.isNotBlank(bo.getGatheTableName()), GatheTable::getGatheTableName, bo.getGatheTableName());
        lqw.eq(StringUtils.isNotBlank(bo.getGatheTableColumn()), GatheTable::getGatheTableColumn, bo.getGatheTableColumn());
        lqw.eq(StringUtils.isNotBlank(bo.getGatheFormula()), GatheTable::getGatheFormula, bo.getGatheFormula());
        lqw.eq(StringUtils.isNotBlank(bo.getGatheFormulaValue()), GatheTable::getGatheFormulaValue, bo.getGatheFormulaValue());
        return lqw;
    }

    /**
     * 新增采集数据主
     *
     * @param bo 采集数据主
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(GatheTableBo bo) {
        GatheTable add = MapstructUtils.convert(bo, GatheTable.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采集数据主
     *
     * @param bo 采集数据主
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(GatheTableBo bo) {
        GatheTable update = MapstructUtils.convert(bo, GatheTable.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GatheTable entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除采集数据主信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
